package com.kind.basketball.basketballappactivity.Activity.test;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageView;

import com.kind.basketball.basketballappactivity.Activity.MainBasketballActivity;
import com.kind.basketball.basketballappactivity.R;

import java.util.Timer;
import java.util.TimerTask;

public class SlideshowView  extends AppCompatActivity



{

    private Gallery mGallery;
    private int index = 0;// 记录选中的图片位置
    private ImageView[] mImageViewIds;// 小圆点ImageView数组
    private static final int IMAGE_COUNT = 3;// 小圆点个数

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_slideshow);

        findViews();
        mImageViewIds[0].setImageDrawable(getBaseContext().getResources()
                .getDrawable(R.drawable.dot_focus));
        ImageAdapter adapter = new ImageAdapter(this);
        mGallery.setAdapter(adapter);
        Timer timer = new Timer();
        timer.schedule(task, 2000, 2000);
        mGallery.setOnItemSelectedListener(onItemSelectedListener);
        mGallery.setOnItemClickListener(onItemClickListener);
    }

    private void findViews() {
        mGallery = (Gallery) findViewById(R.id.gallery);
        mImageViewIds = new ImageView[] { (ImageView) findViewById(R.id.dot_1),
                (ImageView) findViewById(R.id.dot_2),
                (ImageView) findViewById(R.id.dot_3) };
    }

    private TimerTask task = new TimerTask() {

        @Override
        public void run() {
            Message message = new Message();
            message.what = 2;
            index = mGallery.getSelectedItemPosition();
            index++;
            handler.sendMessage(message);
        }
    };

    /**
     * 开一个线程执行耗时操作
     */
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 2:
                    mGallery.setSelection(index);
                    break;
                default:
                    break;
            }
        }

    };
    /**
     * 设置小圆点显示，position会一直增加，如果要循环显示图片，需要对position取余，否则数组越界
     */
    private AdapterView.OnItemSelectedListener onItemSelectedListener = new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                                   int position, long id) {
            int pos = position % IMAGE_COUNT;
            mImageViewIds[pos].setImageDrawable(getBaseContext().getResources()
                    .getDrawable(R.drawable.dot_focus));
            if (pos > 0) {
                mImageViewIds[pos - 1].setImageDrawable(getBaseContext()
                        .getResources().getDrawable(R.drawable.dot_blur));
            }
            if (pos < (IMAGE_COUNT - 1)) {
                mImageViewIds[pos + 1].setImageDrawable(getBaseContext()
                        .getResources().getDrawable(R.drawable.dot_blur));
            }
            if (pos == 0) {
                mImageViewIds[IMAGE_COUNT - 1]
                        .setImageDrawable(getBaseContext().getResources()
                                .getDrawable(R.drawable.dot_blur));
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    };

    /**
     * 点击事件，点击图片进入SecondActivity
     */
    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
                                long arg3) {
            Intent intent = new Intent();
            intent.setClass(SlideshowView.this, MainBasketballActivity.class);
            startActivity(intent);
        }
    };
}

